<?php
/**
 * Fastapp.
 * 7/13/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp;

use App\Fastapp\Model\FastappAppSetting;
use App\Setting\Mapper\SettingConfigMapper;
use App\System\Mapper\SystemDictDataMapper;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\Annotation\CacheEvict;
use Hyperf\Di\Annotation\Inject;

class FastappApiCacheService
{
    #[Inject]
    public SettingConfigMapper $configMapper;
    #[Inject]
    public SystemDictDataMapper $dictMapper;

    #[Cacheable(prefix: 'api:cache:config', value: '_#{key}')]
    public function getConfigByGroupKey(string $key): ?array
    {
        return array_filter(array_column($this->configMapper->getConfigByGroupKey($key), 'value', 'key'));
    }

    #[CacheEvict(prefix: 'api:cache:config', all: true)]
    public function clearConfig(): bool
    {
        return true;
    }

    #[Cacheable(prefix: 'api:cache:dict', value: '_#{key}')]
    public function getDictByKey(string $key): ?array
    {
        return $this->dictMapper->model::query()->select(['label', 'value'])->where(['code' => $key, 'status' => 1])->orderByDesc('sort')->get()->toArray();
    }

    #[CacheEvict(prefix: 'api:cache:dict', all: true)]
    public function clearDict(): bool
    {
        return true;
    }

    #[Cacheable(prefix: 'api:cache:app_setting', value: '_#{key}')]
    public function getAppSetting(string|int $key): ?array
    {
        return FastappAppSetting::query()->where(['account_type' => $key])->pluck('val', 'key')->toArray();
    }

    #[CacheEvict(prefix: 'api:cache:app_setting', value: '_#{key}')]
    public function clearAppSetting(string|int $key): bool
    {
        return true;
    }

    /**
     * 清除缓存.
     */
    #[CacheEvict(prefix: 'api:cache', all: true)]
    public function clearCache(): bool
    {
        return true;
    }
}